# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.

import cv2 as cv
import numpy as np
import point


def resize_img(image, height=1800):
    h, w = image.shape[:2]
    pro = height / h
    s_w = int(w * pro)
    s_h = int(height)
    size = (s_w, s_h)
    img = cv.resize(image, size)
    return img, s_h, s_w, pro


def line_detection(img):
    """方法一"""
    gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    edges = cv.Canny(gray, 50, 150, apertureSize=3)
    lines = cv.HoughLines(edges, 1, np.pi / 180, 200)
    # 以下为标准做法
    for line in lines:
        rho, theta = line[0]
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * a)
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * a)
        cv.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
    return img


def line_detect_possible(img):
    """方法二"""
    gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    edges = cv.Canny(gray, 50, 150, apertureSize=3)
    # minLineLength：线段最大长度
    # maxLineGap:点和线段之间允许的间隔大小
    lines = cv.HoughLinesP(edges, 1, np.pi / 180, 200, minLineLength=50, maxLineGap=10)
    for line in lines:
        x1, y1, x2, y2 = line[0]
    return img


path = '/Users/wanggh/Desktop/a.jpeg'
# path = '/Users/wanggh/Desktop/original.jpeg'
# path = '/Users/wanggh/Desktop/b.jpeg'

default_pro = 0.76
src = cv.imread(path)

# src, src_h, src_w, src_pro = resize_img(src)

cv.imshow("test", line_detection(src))
cv.waitKey(0)
cv.destroyAllWindows()
